<template>
  <div class="home">
    <el-form
      status-icon
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input type="password" v-model="ruleForm.password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import login from '../../models/login'
export default {
  name: "Home",
  data() {
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("密码不能为空"));
      } else {
        /* 数字,大小写字母,特殊字符组成长度不能小于6 */
        if (value.length < 5) {
          callback(new Error("密码长度不能小于5位"));
        } else {
          callback();
        }
      }
    };
    return {
      ruleForm: {
        username: "",
        password: ""
      },
      //表单验证规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 5, message: "用户名最少是5个字符", trigger: "blur" }
        ],
        password: [{ required: true, validator: validatePass }]
      }
    };
  },
  methods: {
    async submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
            // HTTP
            var res = await login(this.ruleForm);
            alert(res.data.msg);
            if(res.data.code==200){
                this.$router.push("/")
            }
            // 设置uvex控制，登录按钮是否显示
            this.$store.commit('setState',res.data.id)
        } else {
            return false;
        }
      });
    }
  }
};
</script>
<style scoped>
</style>